package com.idev.module.user.controller;

import com.idev.common.util.SendEmailUtil;
import com.idev.module.user.domain.User;
import com.idev.common.entiy.SysEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.context.Context;

/**
 * Created by yigeyanse on 2017/6/16.
 */
@RestController
public class HelloController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/redis")
    public String index() {
        // 保存字符串
        stringRedisTemplate.opsForValue().set("aaa", "111");
        String string = stringRedisTemplate.opsForValue().get("aaa");
        System.out.println(string);

        // 保存对象
        User user = new User("超人", 20);
        redisTemplate.opsForValue().set(user.getUserName(), user);

        user = new User("蝙蝠侠", 30);
        redisTemplate.opsForValue().set(user.getUserName(), user);

        user = new User("蜘蛛侠", 40);
        redisTemplate.opsForValue().set(user.getUserName(), user);
        User user1 = (User)redisTemplate.opsForValue().get("超人");
        System.out.println(user1.getAge().longValue());
        System.out.println(((User)redisTemplate.opsForValue().get("蝙蝠侠")).getAge().longValue());
        System.out.println(((User)redisTemplate.opsForValue().get("蜘蛛侠")).getAge().longValue());

        return "Hello World";
    }

    @RequestMapping("/sendEmail")
    public void sendEmail() {
        try {
            SysEmail mail = new SysEmail();
            mail.setTo("1054655820@qq.com");
            mail.setSubject("邮件主题");
            mail.setContents("hahah");
            SendEmailUtil.sendSimpleMail(mail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/sendHtmlMail")
    public void sendHtmlMail() {
        try {
            SysEmail mail = new SysEmail();
            mail.setTo("1054655820@qq.com");
            mail.setSubject("邮件主题");
            StringBuffer sb = new StringBuffer();
            sb.append("<h1>大标题-h1</h1>")
                    .append("<p style='color:#F00'>红色字</p>")
                    .append("<p style='text-align:right'>右对齐</p>");
            mail.setContents(sb.toString());
            SendEmailUtil.sendHtmlMail(mail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/sendTemplateEmail")
    public void sendTemplateEmail() {
        try {

            SysEmail mail = new SysEmail();
            Context data = new Context();
            data.setVariable("username", "正阳");
            data.setVariable("age", "23岁");
            data.setVariable("context", "正阳是个好孩子");

            mail.setData(data);
            mail.setTo("zhengyang.wang@weigaogroup.com");
            mail.setSubject("邮件主题");
            mail.setContents("hahah");
            mail.setTemplateName("hello");

            SendEmailUtil.sendTemplateMail(mail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
